/**
 * Gulp Build: npm + gulp
 *
 * Quick Doc: http://www.cnblogs.com/jadeboy/p/4995864.html
 *
 * Install ALL:
 *     cnpm install
 * Install gulp:
 *     http://www.gulpjs.com
 *     npm install --global gulp
 * Install cnpm:
 *     https://npm.taobao.org
 *     npm install -g cnpm --registry=https://registry.npm.taobao.org
 *
 * Require Plugin:
 *     gulp                 --- 自动化构建工具
 *     gulp-load-plugins    --- 自动插件加载
 *     gulp-browserify      ---
 *     gulp-ruby-sass       ---
 *     gulp-autoprefixer    ---
 *     gulp-rename          ---
 *     gulp-minify-css      ---
 *     gulp-connect         --- 本地Web Server
 *     gulp-livereload      --- 自动刷新浏览器
 *
 * Usage:
 *     gulp default or gulp
 */

// 所需要的模块
// 编译Sass (gulp-ruby-sass)
// Autoprefixer (gulp-autoprefixer)
// 缩小化(minify)CSS (gulp-minify-css)
// JSHint (gulp-jshint)
// 拼接 (gulp-concat)
// 丑化(Uglify) (gulp-uglify)
// 图片压缩 (gulp-imagemin)
// 即时重整(LiveReload) (gulp-livereload) XXX
//  browser-sync
// 清理档案 (gulp-clean)
// 图片快取，只有更改过得图片会进行压缩 (gulp-cache)
// 更动通知 (gulp-notify)
// html页面压缩(gulp-htmlmin)
// 重命名 (gulp-rename)
// 执行bower安装(gulp-bower)
// 加载所有插件 (gulp-load-plugins)
// 模板引擎库 (gulp-handlebars)
// gulp-browserify

'use strict';

var gulp = require("gulp"),
    gulploadplugins = require("gulp-load-plugins"),
    plugins = gulploadplugins(),
    browserify = require("gulp-browserify"),
    gulp_sass = require('gulp-sass'),
    autoprefixer = require("gulp-autoprefixer"),
    rename = require("gulp-rename"),
    minifycss = require("gulp-minify-css"),
    connect = require("gulp-connect"),
    group = require('gulp-group-files'),
    livereload = require("gulp-livereload");


var _APP_PATH = {
    'APP_PRODUCTION_SRC': './build/',
    'APP_PRODUCTION_CSS_SRC': './build/style',
    'APP_PRODUCTION_IMAGE_SRC': './build/style',
    'APP_PRODUCTION_JS_SRC': './build/'
};

var _APP_DEBUG_SRC = './src/';
var _APP_STYLE_IMG = './src/style/images/**/*';
var _APP_STYLE_SASS = './src/style/**/*.scss';
var _APP_INDEX_JS = './src/app.js';

var _APP_STYLE_SASS_SET = {
    'MAIN': {
        'FROM_PATH': './src/style/main.scss',
        'TO_PATH': './src/style/'
    }
};

var _APP_JS = './build/app.js'


gulp.task('APP_STYLE_SASS', function () {
    return group(_APP_STYLE_SASS_SET, function (key, sass_file_set) {
          return gulp.src(sass_file_set.FROM_PATH)
              .pipe(gulp_sass().on('error', gulp_sass.logError))
              .pipe(gulp.dest(sass_file_set.TO_PATH));
      })();
});


gulp.task('APP_STYLE_SASS_BUILD', function () {
    gulp.watch(_APP_STYLE_SASS, ['APP_STYLE_SASS'])
});


gulp.task("APP_STYLE_CSS_BUILD", ["APP_STYLE_SASS_BUILD"], function () {
    return gulp.src("./src/style/main.css")
               .pipe(autoprefixer('last 2 version', 'safari 5', 'ie8', 'ie9', 'opera12.1'))
               .pipe(gulp.dest("./build/style"))
               .pipe(rename({suffix:'.min'}))
               .pipe(minifycss())
               .pipe(gulp.dest("./build/style"));
});


gulp.task("APP_STYLE_BASE_CSS_BUILD", ["APP_STYLE_SASS_BUILD"], function () {
    return gulp.src("./src/style/base.css")
        .pipe(autoprefixer('last 2 version', 'safari 5', 'ie8', 'ie9', 'opera12.1'))
        .pipe(gulp.dest("./build/style"))
        .pipe(rename({suffix:'.min'}))
        .pipe(minifycss())
        .pipe(gulp.dest("./build/style"));
});


gulp.task("APP_STYLE_CSS_WATCH", ["APP_STYLE_CSS_BUILD", "APP_STYLE_BASE_CSS_BUILD"], function () {
    gulp.watch("./src/style/*/*.css", ["APP_STYLE_CSS_BUILD", "APP_STYLE_BASE_CSS_BUILD"]);
});


gulp.task("APP_HTML_BUILD", function () {
    return gulp.src("./src/index.html").pipe(gulp.dest("./build"));
});


gulp.task("APP_JS_INDEX", function () {
    gulp.src(_APP_INDEX_JS)
        .pipe(browserify({transform: 'reactify'}))
        .pipe(gulp.dest(_APP_PATH.APP_PRODUCTION_JS_SRC))
        .pipe(rename({suffix: '.min'}))
        .pipe(plugins.uglify())
        .pipe(gulp.dest(_APP_PATH.APP_PRODUCTION_JS_SRC));
});


gulp.task("APP_STYLE_IMAGE", function () {
    return gulp.src('./src/style/images').pipe(gulp.dest(_APP_PATH.APP_PRODUCTION_IMAGE_SRC));
});


gulp.task("APP_BUILDER_SERVER", function () {
    connect.server();
});


gulp.task("APP_JS_INDEX_WATCH", ["APP_JS_INDEX"], function () {
    gulp.watch("./src/**/*.js", ["APP_JS_INDEX"]);
});


gulp.task("APP_HTML_BUILD_WATCH", ["APP_HTML_BUILD"], function () {
    gulp.watch("./src/index.html", ["APP_HTML_BUILD"]);
});


// "APP_STYLE_IMAGE"
gulp.task("APP_BUILDER", ["APP_STYLE_CSS_WATCH", "APP_JS_INDEX_WATCH", 'APP_HTML_BUILD_WATCH']);


gulp.task("default", ["APP_BUILDER_SERVER", "APP_BUILDER"]);
